<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Samples\Queue\CreateQueueAndSendMessage;
use App\Models\Device;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\AdminUser;
use App\Models\ProjectPlan;
use Illuminate\Support\Facades\Redis;

class SmokeApiController extends Controller{
	public function deviceInfo(Request $request){ //上线
// 			$data['Alarm'] = 529;
// 			$alarm = decbin($data['Alarm']);
// 			$newStr= sprintf('%012s', $alarm);
// 			$newStr = str_split($newStr);
// 			print_r(array_reverse($newStr));die;
		$info = $request->all();
		Log::info('j');
		Log::info($info);
		if($info['deviceInfo']['status']=='ONLINE'||$info['deviceInfo']['status']=='OFFLINE'){
		    $status = $info['deviceInfo']['status']=='ONLINE'?'1':'0';
		    DB::table('xfy_device')->where('gatewayId', $info['gatewayId'])->update(['heartbeat'=>$status]);
		}
		if($info['deviceInfo']['status']=='ABNORMAL'){//设备异常
		    DB::table('xfy_device')->where('gatewayId', $info['gatewayId'])->update(['heartbeat'=>4]);
		}



		// $data = file_get_contents('php://input');
		// print_r($data);die;
		// Log::info($data);
		// Db::table('xfy_device')->where(['gatewayId'=>$infos['gatewayId']])->update(['status'=>$infos['deviceInfo']['status'],'updated_at'=>date("Y-m-d H:i:s")]);
// 		Log::info($infos);
		// print_r(123);die;
		// Log::info($infos);
	}

	public function deviceData(Request $request){ //报警
		$infos = $request->all();
		// $infos = array (
		// 	'notifyType' => 'deviceDataChanged',
		// 	'deviceId' => 'ec9588b8-4d9a-44a8-8e66-c91af26e7bcf',
		// 	'gatewayId' => 'ec9588b8-4d9a-44a8-8e66-c91af26e7bcf',
		// 	'requestId' => NULL,
		// 	'service' => 
		// 	array (
		// 	  'serviceId' => 'Smoke',
		// 	  'serviceType' => 'Smoke',
		// 	  'data' => 
		// 	  array (
		// 		'deviceType' => 1,
		// 		'imei' => '869512030307544',
		// 		'iccid' => '89861118287000418948',
		// 		'imsi' => '460113031140788',
		// 		'timestamp' => 1643077381,
		// 		'nbVersion' => 'L1529.4.01.05.NP',
		// 		'cellId' => 99731792,
		// 		'pci' => 203,
		// 		'rsrp' => -63,
		// 		'snr' => 30,
		// 		'csq' => 27,
		// 		'tac' => 24387,
		// 		'ci' => 99731792,
		// 		'act' => 1,
		// 		'tauPeriod' => 56,
		// 		'reportType' => 3,
		// 		'eventType' => 3,
		// 		'reportPeriod' => 0,
		// 		'emergencyReport' => 0,
		// 		'faultTag' => 0,
		// 		'register' => 
		// 		array (
		// 		  'silenceStatus' => 0,
		// 		  'almStatus' => 0,
		// 		  'sensiFault' => 0,
		// 		  'powerStatus' => 0,
		// 		  'simualmStatus' => 1,
		// 		  'linkalmStatus' => 0,
		// 		  'unnormalStatus' => 0,
		// 		  'netStatus' => 1,
		// 		  'pingStatus' => '1',
		// 		  'cmdAlarm' => '0',
		// 		  'cmdLink' => '0',
		// 		  'cmdOnline' => '1',
		// 		  'cmdTest' => '0',
		// 		  'cmdLed' => '0',
		// 		  'cmdMute' => '0',
		// 		),
		// 		'sensorVersion' => 
		// 		array (
		// 		  'versionProtocal' => 1,
		// 		  'versionHardware' => 2,
		// 		  'versionFirmWare' => 5,
		// 		),
		// 		'removeStatus' => 1,
		// 		'unnormalIndication' => 0,
		// 		'unnormalTime' => 0,
		// 		'samleFreqency' => 0,
		// 		'emergencySample' => 0,
		// 		'smokeScope' => 0.01,
		// 		'smokeThresold' => 0,
		// 		'powerScope' => 312,
		// 		'powerThresold' => 0,
		// 		'frequencyPoint' => 0,
		// 		'extra' => 
		// 		array (
		// 		),
		// 	  ),
		// 	  'eventTime' => '20220125T022303Z',
		// 	),);
		Log::info('L');
		Log::info($infos);
		
// 		$data['Alarm'] = 1;
		// $data['imei'] = '868474049209404';
		// $data = file_get_contents('php://input');
		// Log::info($data);
		// die;
		$data = $infos['service']['data'];
		if(isset($data['smoke'])){ //川大
			$imei = substr($data['smoke'],2,15); //imei
			$event = hexdec(substr($data['smoke'],70,2)); //报警事件
			$update['signal'] = substr($data['smoke'],50,2); //信号强度
			$update['batvoltage_ai5'] = hexdec(substr($data['smoke'],52,4)); //电池电压

			// $users = DB::table('xfy_device')->where(['xfy_device.cid'=>$imei])
	            // ->leftJoin('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')
	            // ->get()->toArray();
	            		$device = Device::where(['cid'=>$imei])->first();
	            		$userarr = ProjectUser::where(['projectId'=>$device['projectId']])->select('user_id')->get()->toArray();
	            		$unionid = AdminUser::whereIn('id',$userarr)->select('unionid')->get()->toArray();

			// $user = Db::table('admin_users')->where(['id'=>$users[0]['user_id']])->orWhere('pid', '=', $users[0]['user_id'])->get()->toArray();
			// $ids = array_column($user, 'unionid');
			$openid_list = DB::table('users')->whereIn('unionid', $unionid)->select('openid')->get()->toArray();
			// $openid_list = array_column($union,'openid');
			switch ($event) {
				case 55: //火警
					$update['almStatus'] = 1;
					$update['status'] = 1;
					$this->sendMsg($users,'alm',$openid_list,'火警','almStatus');
					break;
				case 38: //电池欠压
					$update['powerStatus'] = 1;
					$update['status'] = 1;
					$this->sendMsg($users,'fault',$openid_list,'电量不足','powerStatus');
					break;
				case 23: //传感器故障
					$ipdate['unnormalStatus'] = 1;
					$update['status'] = 1;
					$this->sendMsg($users,'fault',$openid_list,'非正常指示状态','unnormalStatus');
					break;
			}
			Db::table('xfy_device')->where(['cid'=>$imei])->update($update);
		}elseif(isset($data['Alarm'])){ //输入输出
			$imei = $data['imei'];
			$users = DB::table('xfy_device')->where(['xfy_device.cid'=>$imei])
		            ->leftJoin('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')
		            ->get()->toArray();
			$user = Db::table('admin_users')->where(['id'=>$users[0]['user_id']])->orWhere('pid', '=', $users[0]['user_id'])->get()->toArray();
			$ids = array_column($user, 'unionid');
			$union = DB::table('users')->whereIn('unionid', $ids)->get()->toArray();
			$openid_list = array_column($union,'openid');
			$info['alarm'] = $data['Alarm'];
			$info['di'] = $data['DI'];
			$info['current_ai1'] = $data['Current_AI1'];
			$info['current_ai2'] = $data['Currrent_AI2'];
			$info['voltage_ai3'] = $data['Voltage_AI3'];
			$info['voltage_ai4'] = $data['Voltage_AI4'];
			$info['batvoltage_ai5'] = $data['BatVoltage_AI5'];
			if($data['Alarm']>0){
				$info['status'] = 1;
			}
			Db::table('xfy_device')->where(['cid'=>$imei])->update($info);
			$alarm = decbin($data['Alarm']);
			$newStr= sprintf('%012s', $alarm);
			$arr = array_reverse(str_split($newStr));
		    	foreach ($arr as $key => $value){
		    		if($value==1){
		    			if($key<4){//开关量
		    				$cid = 'DI'.($key+1);
		    				$msg = 'DI'.($key+1).'报警';
			    		}
			    		if($key>=4&&$key<8){ //低于下限阈值
			    			$cid = 'AI'.($key-3);
			    			$msg = 'AI'.($key-3).'低于下限阈值';
			    		}
			    		if($key>=8){ //高于上限阈值
			    			$cid = 'AI'.($key-7);
			    			$msg = 'AI'.($key-7).'高于上限阈值';
			    		}
			    		Db::table('xfy_device')->where(['pid'=>$users[0]['deviceId'],'cid'=>$cid])->update(['alarm'=>1]);
			    		Log::info($cid.$msg);
			    		$this->sendMsg($users,'Alarm',$openid_list,$msg,$cid);
		    		}
		    	}

		}else{ //霍尼韦尔
			$imei = $data['imei'];
			// $imei = '869512030310621';
// 			Log::info($imei);
			$update['batvoltage_ai5'] = $data['csq'];
			// $users = DB::table('xfy_device')->where(['xfy_device.cid'=>$imei])
	        //     ->leftJoin('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')
	        //     ->get()->toArray();
	        // Log::info($users);
			$device = Device::where(['cid'=>$imei])->first();
			$project = Project::where(['projectId'=>$device['projectId']])->first();
			$param['deviceId'] = $device['deviceId'];
			$param['imei'] = $imei;
			$param['projectname'] = $project['projectname'];
			$param['address'] = $device['address'];
			$userarr = ProjectUser::where(['projectId'=>$device['projectId']])->select('user_id')->get()->toArray();
			$unionid = AdminUser::whereIn('id',$userarr)->select('unionid')->get()->toArray();
			$openid_list = DB::table('users')->whereIn('unionid', $unionid)->select('openid')->get()->toArray();
			// print_r($openid_list);die;
			// $user = Db::table('admin_users')->where(['id'=>$users[0]['user_id']])->orWhere('pid', '=', $users[0]['user_id'])->get()->toArray();
			// $ids = array_column($user, 'unionid');
			// $union = DB::table('users')->whereIn('unionid', $ids)->get()->toArray();
			// $openid_list = array_column($union,'openid');
			// Log::info($openid_list);
			
			if($data['reportType']==6){
				$datar['type'] = 'Test';
				$this->sendMsg($param,'fault',$openid_list,'设备测试','Test');
			}else if($data['reportType'] == 4){
				if($data['removeStatus'] == 1){
					$update['removeStatus'] = 1;
					$update['status'] = 2;
					$datar[] = '设备开盖';
					$this->sendMsg($param,'fault',$openid_list,'设备开盖','removeStatus');
				}
			}else if($data['smokeThresold'] > 0.15){
				$update['smokeThresold'] = 1;
				$update['status'] = 1;
				$datar[] = '烟雾报警';
				$this->sendMsg($param,'alm',$openid_list,'烟雾报警','smokeThresold');
			}
			$register = $data['register'];
			if($register['netStatus'] == 1){
			    $update['status'] = 1;//'在线'
			}
			if ($register['simualmStatus'] == 1){
				$update['simualmStatus'] = 1;
				$update['status'] = 1;
				$datar[] = '模拟火警';
				$this->sendMsg($param,'alm',$openid_list,'模拟火警','simualmStatus');
			}
			if($register['unnormalStatus'] == 1){
				$update['unnormalStatus'] = 1;
				$update['status'] = 2;
				$datar[] = '非正常指示状态';
				$this->sendMsg($param,'fault',$openid_list,'非正常指示状态','unnormalStatus');
			}
			if($register['sensiFault'] == 1){
				$update['sensiFault'] = 1;
				$update['status'] = 2;
				$datar[] = '灵敏度故障';
				$this->sendMsg($param,'fault',$openid_list,'灵敏度故障','sensiFault');
			}
			if($register['powerStatus'] == 1){
				$update['powerStatus'] = 1;
				$update['status'] = 2;
				$datar[] = '电量不足';
				$this->sendMsg($param,'fault',$openid_list,'电量不足','powerStatus');
			}
			if($register['linkalmStatus'] == 1){
				$update['linkalmStatus'] = 1;
				$update['status'] = 1;
				$datar[] = '联动火警';
				$this->sendMsg($param,'alm',$openid_list,'联动火警','linkalmStatus');
			}
			if($register['almStatus'] == 1){
				$update['almStatus'] = 1;
				$update['status'] = 1;
				$datar[] = '火警';
				$this->sendMsg($param,'alm',$openid_list,'火警','almStatus');
			}
			if(isset($datar)){
				$arr[$imei] = $datar;
				Redis::set($device['projectId'],json_encode($arr));
				$device = Device::where(['cid'=>$imei])->first()->toArray();
				$url = '/json/'.$device['planeJPG'].'.json';
				// $plane = ProjectPlan::where(['id'=>$device['planeJPG']])->first()->toArray();
				if(isset($url)){ //存在文件
					$json = file(public_path($url));
					$point = json_decode($json[0]);
					foreach($point as $key => &$value){
						$val = json_decode($value,1);
						if($val['name']==$imei){
							$val['type'] = 3;
						}
						$value = json_encode($val);
					}
					$file = public_path('/json/' . $device['planeJPG'].'.json');
					file_put_contents($file, json_encode($point));
					// ProjectPlan::where(['id'=>$info['id']])->update(['jsonurl'=>'/json/' . $info['id'].'.json']);
				}
			}
			//按项目id为key 来存下面的设备
			// Db::table('xfy_device')->where(['cid'=>$imei])->update($update);
		}
	}
	public function sendMsg($param,$type,$openid_list,$msg,$detail){
		$time = date("Y-m-d H:i:s");
		$info['deviceId'] = $param['deviceId'];
		$info['type'] = $type;
		$info['msg'] = $msg;
		$info['created_at'] = $time;
		$info['device_detail'] = $detail;

		Db::table('xfy_device_log')->insert($info);
		// $param['deviceId'];//设备id
		// $param['projectId'];//项目id
		// $param['projectname'];//项目名称
		// $param['address'];//设备地址
         $app = app('wechat.official_account');
         foreach ($openid_list as $key=>$value){
         //$template_id['alm'] = 'LIpTip4j-gTGA_yjFov3aUqqRc9ATNY0iTy11I4k0wU'; //火警信息通知 2017年11月11日16：00请尽快处理确定报警设备
         //$template_id['fault'] = 'wKqlkfSZPuy3G6iRzVAmlPtTVUvYDuN9WNuTNivMYpY'; //设备报警通知 设备报警xxxx请注意关注设备问题，及时解决！
         switch ($type) {
         	case 'alm':
         		$res = $app->template_message->send([
			        'touser' => $value->openid,
			        'template_id' => 'LIpTip4j-gTGA_yjFov3aUqqRc9ATNY0iTy11I4k0wU',
			        'miniprogram' => [
			                'appid' => 'wx301bce58badb7cc3',
			                'pagepath' => 'pages/message/message',
			        ],
			        'data' => [
			        	"first" => [
			        	    "value"=>"您的设备报警了",
		                    	"color"=>"#173177"
			        	],
			        	"keyword1" => [
		                    "value"=>$param['projectname'],
		                    "color"=>"#173177"
		                ],
		                "keyword2"=> [
		                	"value"=>$param['imei'],
		                    "color"=>"#173177"
		                ],
		                "keyword3"=> [
		                	"value"=>$param['address'],
		                    "color"=>"#173177"
		                ],
		                "keyword4"=> [
		                	"value"=>$time,
		                    "color"=>"#173177"
		                ],
		                "remark" => [
		                	"value"=>'请尽快处理确定报警设备',
		                	"color"=>"#173177"
		                	]
			        ],
			    ]);
         		break;
         	case 'fault':
         		$res = $app->template_message->send([
		        'touser' => $value->openid,
		        'template_id' => 'wKqlkfSZPuy3G6iRzVAmlPtTVUvYDuN9WNuTNivMYpY',
		        'miniprogram' => [
		                'appid' => 'wx301bce58badb7cc3',
		                'pagepath' => 'pages/message/message',
		        ],
		        'data' => [
		        	"first" => [
		        		"value"=>'设备出现异常请及时关注',
		        		"color"=>"#173177"
		        	],
		        	"keyword1" => [
		                   "value"=>$time,
		                   "color"=>"#173177"
		                ],
		                "keyword2"=> [
		                   "value"=>$param['imei'],
		                   "color"=>"#173177"
		                ],
		                "keyword3"=> [
		                   "value"=>'项目：'.$param['projectname'].',设备状态：'.$msg,
		                   "color"=>"#173177"
		                ],
		                "remark" => [
		                	"value"=>'请注意关注设备问题，及时解决！',
		                	"color"=>"#173177"
		                ]
			        ],
			    ]);
         		break;
         		case 'Alarm':
         		$res = $app->template_message->send([
			        'touser' => $value->openid,
			        'template_id' => 'wKqlkfSZPuy3G6iRzVAmlPtTVUvYDuN9WNuTNivMYpY',
			        'miniprogram' => [
			                'appid' => 'wx301bce58badb7cc3',
			                'pagepath' => 'pages/message/message',
			        ],
			        'data' => [
			        	"first" => [
			        		"value"=>'设备报警请及时处理',
			        		"color"=>"#173177"
			        	],
			        	"keyword1" => [
		                   "value"=>$time,
		                   "color"=>"#173177"
		                ],
		                "keyword2"=> [
		                   "value"=>$param['imei'],
		                   "color"=>"#173177"
		                ],
		                "keyword3"=> [
		                   "value"=>$param['projectname'].','.$msg,
		                   "color"=>"#173177"
		                ],
		                "remark" => [
		                	"value"=>'请注意关注设备问题，及时解决！',
		                	"color"=>"#173177"
		                ]
			        ],
			    ]);
			    break;
         		}
         }
    }
    public function test(){
        $str = '0a8653520362445710001b978986111826700813087700010131015e002e10e0010105f20c02';
        $imei = substr($str,2,15); //imei
		$event = hexdec(substr($str,70,2)); //报警事件
		$update['signal'] = substr($str,50,2); //信号强度
		$update['batvoltage_ai5'] = hexdec(substr($str,52,4)); //电池电压
		$update['heart'] = hexdec('10e0');
		print_r($update);die;
    }
    public function test1(){
		$data = [
			'template_id' => '890vAF2WMqWtWb6s3lkHXgfkWnHJX499DtAtwpyJrEI', // 所需下发的订阅模板id
			'touser' => 'oLFaq5QkI_6L5oKO00LQX2YMsvJs',     // 接收者（用户）的 openid
			'page' => '',       // 点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
			'data' => [         // 模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
				'date1' => [
					'value' => date('Y-m-d H:i',time()),
				],
				'thing2' => [
					'value' => '测试测试',
				],
				'thing3' => [
					'value' => '测试测试',
				],
			],
		];
		$mini = \EasyWeChat::miniProgram();

		$res = $mini->subscribe_message->send($data);
		print_r($res);die;
        // $a = app('wechat.mini_program');
        // $accessToken = $a->access_token; // EasyWeChat\Core\AccessToken 实例
        // $token = $accessToken->getToken();
        // $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
        // $data['scene'] = ['id'];
        // print_r($token);die;
        // $a = date('H:i');
        // $b = '16:50';
        // if($a>$b){
        //     echo 111;die;
        // }
        // print_r(date('H:i'));die;
//     	//拉取
//         $accessId = "LTAIXidsHeo1vYeo";
//         $accessKey = "vO9c9Yqyha6PNAkFyb4jAQPfJ5rvbU";
//         $endPoint = "https://1960429774320704.mns.cn-shanghai.aliyuncs.com/";
//         require '../vendor/aliyun/aliyun-mns-php-sdk/Samples/Queue/CreateQueueAndSendMessage.php';
//     	$c = new CreateQueueAndSendMessage($accessId,$accessKey,$endPoint);

//     	print_r($c);die;

        //iot添加设备
        // require '../vendor/iot_sdk/Test.php';
        // $t = new \Test();
        // $productKey = 'a1zQYVHQ92L';
        // $deviceName = $cid;
        // $result = $t->registDeviceTest($productKey, $deviceName);
        // print_r($result);die;
//     	//创建小程序带参数二维码 10_1 (10为项目Id,1为设备Id)
//     	$app = app('wechat.mini_program');
//     	$response = $app->app_code->getUnlimit('10_1', [
// 		    'page'  => 'pages/authorize/authorize',
// 		    'width' => 600,
// 		]);
		// $directory = 'images/QRcode/';
// 		// if (!is_dir($directory)) {
// 		// 	print_r(1);die;id
//   //          mkdir($directory, 0755, true); // @codeCoverageIgnore
//   //      }
// 		print_r($response);
// 		if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
// 		    $filename = $response->saveAs('images/QRcode/', 'id.png');
// 		}
// 		print_r($filename);die;


  //  	$imei = '868474049209404';
		// 	$users = DB::table('xfy_device')->where(['xfy_device.cid'=>$imei])
	 //           ->leftJoin('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')
	 //           ->get()->toArray();
	 //           print_r($users);die;
  //  	//shuru
  //  	$str = decbin(529);
  //  	//字符串补全
  //  	$info = sprintf('%012s', $str);
  //  	$arr = array_reverse(str_split($info));
  //  	foreach ($arr as $key => $value){
  //  		if($value==1){
  //  			if($key<4){//开关量
  //  				$cid = 'AI'.$key+1;
	 //   		}
	 //   		if($key>=4&&$key<8){ //低于下限阈值
	 //   			$cid = 'DI'.$key-3;
	 //   		}
	 //   		if($key>=8){ //高于上限阈值
	 //   			$cid = 'DI'.$key-7;
	 //   		}
	 //   		$info['deviceId'] = $deviceId; //设备id
	 //   		// $info['type'] = ; //报警类型
	 //   		// $info['msg'] = ; //
	 //   		Db::table('xfy_device_log')->insert($info);
  //  		}
  //  	}

  //  	print_r(array_reverse($arr1));die;
  //  	$smoke = '0a86535203632105600000cf8986111826700813068700010117015e002e10e001010537713f';
		// $xinhao = substr($smoke,50,2);
		// $batvoltage_ai5 = hexdec(substr($smoke,52,4));
		// print_r($xinhao);
		// print_r($batvoltage_ai5);die;
    	// $imei = '868474049209404';
		$imei = '869512030307544';
		$users = Device::where(['xfy_device.cid'=>$imei])
	            ->leftJoin('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')
	            ->get()->toArray();
	           // print_r('pages/authorize/authorize?id='.$users[0]['deviceId'].'&projectId='.$users[0]['projectId']);die;
	           //os2gqwGkZJN30wK2YockqJdRTgPw
	           //os2gqwJxX8OXyhuoJxQ5HL_zr_ww
	    $app = app('wechat.official_account');
	    $res = $app->template_message->send([
	        'touser' => 'os2gqwJxX8OXyhuoJxQ5HL_zr_ww',
	        'template_id' => 'LIpTip4j-gTGA_yjFov3aUqqRc9ATNY0iTy11I4k0wU',
	        'miniprogram' => [
	                'appid' => 'wx301bce58badb7cc3',
	                'pagepath' => 'pages/message/message?id='.$users[0]['deviceId'].'&projectId='.$users[0]['projectId'],
	        ],
	        'data' => [
	        	"first" => [
	        		"value"=>'设备出现异常请及时关注',
	        		"color"=>"#173177"
	        	],
	        	"keyword1" => [
                   "value"=>'2020-04-26 17:08:00',
                   "color"=>"#173177"
                ],
                "keyword2"=> [
                   "value"=>$users[0]['cid'],
                   "color"=>"#173177"
                ],
                "keyword3"=> [
                   "value"=>$users[0]['deviceId'],
                   "color"=>"#173177"
                ],
                "remark" => [
                	"value"=>'pages/authorize/authorize?id='.$users[0]['deviceId'].'&projectId='.$users[0]['projectId'],
                	"color"=>"#173177"
                ]
	        ],
	    ]);
		print_r($res);die;
	    print_r('pages/authorize/authorize?id='.$users[0]['deviceId'].'&projectId='.$users[0]['projectId']);die;
    	$data['smoke'] = '0a86535203632105600000cf8986111826700813068700010117015e002e10e001010537713f';
    	$info['imei'] = substr($data['smoke'],2,15);
		$info['event'] = hexdec(substr($data['smoke'],70,2));
		print_r($info);die;
    }

    public function haikang(){
        //关闭视频加密
        $url = 'https://open.ys7.com/api/lapp/device/encrypt/off';
        $data['accessToken'] = 'at.1vb23apg1s8arvha016tn71b016rkama-3d367onxiq-1unujsr-gnewhhkac';
        $data['deviceSerial'] = 'E04339378';
        $data['validateCode'] = 0;
        $res = $this->send_post($url,$data);
        print_r($res);die;
        $appKey = "74d0e2047e784a28915cdc26e9f1f909";
        $appSecret = "ff7cc0a81c1f6d6ac2051461c80c3d6c";
        $data['accessToken'] = "at.ck231zw456k7r9y6cyu4857jbdm5vnia-838f4bxyhx-05mo29g-y5gpkzskq";
        $url = "https://open.ys7.com/api/lapp/live/video/list";
        $res = $this->send_post($url,$data);
        print_r($res);die;
    }

    // 发送 POST 请求的函数
	  public function send_post($url, $data){
	    $options = array(
	        'http' => array(
	        'method'  => 'POST',
	        'header'  => 'Content-type:application/json',
	        'content' => $data,
	        'timeout' => 60
	        )
	    );
	    $ch = curl_init();//初始化curl
	    curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
	    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
	    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    $result = curl_exec($ch);//运行curl
	    curl_close($ch);
	    return $result;
	  }




}